package com.ym.jiujiangeyou.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import com.ym.jiujiangeyou.R;
import com.ym.jiujiangeyou.TrafficActivity;
import com.ym.jiujiangeyou.bitmap.SimpleImageLoad;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.HttpUtil;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 */
public class MineHistoryFragment extends Fragment {

	private TextView history,history1 ;
	private ImageView mine_history_pic ;
	private Dialog dialog ;
	SimpleImageLoad simpleImageLoad;
	private Toast toast;
	
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.HTTP_REQUEST_ERROR :
				dialog.dismiss() ;
				toast = DialogView.popToast(getActivity(), R.string.http_connect_error) ;
				toast.show() ;
				break;
			case Constants.HTTP_REQUEST_SUCCESS :
				String text = (String) msg.obj ;
				dialog.dismiss() ;
				JSONObject jsonObject = null ;
				try {
					jsonObject = new JSONObject(text);
					history.setText(jsonObject.getString("history")) ;
					history1.setText(jsonObject.getString("history1")) ;
					simpleImageLoad.DisplayImage(jsonObject.getString("url1"), mine_history_pic);
					dialog.dismiss() ;
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					toast = DialogView.popToast(getActivity(), R.string.get_msg_fail) ;
					toast.show() ;
				}
				
				break;
			}
		}
	} ;
	
	
	public MineHistoryFragment() {
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.mine_history, null, false) ;
		init(view) ;
	//	history.setText("ddd") ;
		load(handler) ;
		return view;
		
	}
	
	public void init(View view){
		history = (TextView) view.findViewById(R.id.mine_history) ;
		history1 = (TextView) view.findViewById(R.id.mine_history1) ;
		mine_history_pic = (ImageView) view.findViewById(R.id.mine_history_pic) ;
		simpleImageLoad = new SimpleImageLoad(getActivity());
	}
	
	/** 
	* @param handler  
	* @retur  void 
	* @Description: 请求数据 
	*/
	public void load(final Handler handler){
		dialog = DialogView.popDialog(getActivity(),R.string.isloading) ;
		dialog.show() ;
		new Thread(new Runnable() {
			Message msg = new Message() ;
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String s = HttpUtil.httpGet(Constants.GET_MINE_PUP+1) ;
				if(s.equals(Constants.CONNECTION_TIMEOUT) || s.equals("FAIL")){
					msg.what = Constants.HTTP_REQUEST_ERROR ;
				}else{
					msg.obj = s ;
					msg.what = Constants.HTTP_REQUEST_SUCCESS ;
				}
				handler.sendMessage(msg) ;
			}
		}).start() ;
	}
	
	}

